package org.unicat.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unicat.mapper.StudentMapper;
import org.unicat.mapper.TeacherMapper;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.mapper.UserMapper;
import org.unicat.model.Student;
import org.unicat.model.Teacher;
import org.unicat.model.User;
import org.unicat.model.UserInfo;
import org.unicat.service.StudentService;
import org.unicat.service.TeacherService;
import org.unicat.service.UserRegistService;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRegistService userRegistService;

    //查找所有学生
    @Override
    public List<Student> findAllStudent() {
        return studentMapper.selectAll();
    }

    @Override
    public Student findStudentByName(String name) {
        return studentMapper.selectOne(name);
    }

    @Override
    public UserInfo getStudentInfo(Student student) {
        UserInfo studentInfo = userInfoMapper.selectOne(student.getName());
        studentInfo.setPassword(userMapper.selectOne(student.getName()).getPassword());
        studentInfo.setMajor(studentMapper.selectOne(student.getName()).getMajor());
        return studentInfo;
    }

    @Override
    public void addStudent(UserInfo student) {
        userRegistService.studentRegist(student);
    }

    @Override
    public void updateStudentInfo(UserInfo student) {
        //更新user表
        User user = new User();
        user.setId(userMapper.selectOne(student.getUsername()).getId());
        user.setUsername(student.getUsername());
        user.setPassword(student.getPassword());
        userMapper.updateUser(user);
        //更新信息表
        student.setId(userInfoMapper.selectOne(student.getUsername()).getId());
        userInfoMapper.updateUserInfo(student);
        //更新Student表
        Student student1 = new Student();
        student1.setId(studentMapper.selectOne(student.getUsername()).getId());
        student1.setName(student.getUsername());
        student1.setMajor(student.getMajor());
        studentMapper.updateStudent(student1);
    }

    @Override
    public void deleteStudent(Student student) {
        studentMapper.deleteStudent(student);
        userInfoMapper.deleteUserInfo(userInfoMapper.selectOne(student.getName()));
        userMapper.deleteUser(userMapper.selectOne(student.getName()));
    }



}
